package com.muqi.app.qlearn.mine;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easemob.EMCallBack;
import com.muqi.app.im.qmain.EMHelper;
import com.muqi.app.im.qmain.EmModel;
import com.muqi.app.im.widget.SwitchButton;
import com.muqi.app.im.widget.SwitchButton.OnChangedListener;
import com.muqi.app.project.application.ActivitiesStack;
import com.muqi.app.project.utils.CacheClearUtil;
import com.muqi.app.qlearn.login.LoginActivity;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

public class SettingActivity extends BaseFragmentActivity implements OnClickListener,OnChangedListener {
	private RelativeLayout private_setting_btn;
	private RelativeLayout modify_password_btn;
	private RelativeLayout address_manage_btn;
	private RelativeLayout about_us_btn;
	private long size;
	private TextView cacheTv;
	private SwitchButton notification;
	private SwitchButton sound;
	private SwitchButton vibrate;
	private SwitchButton chat;
	private RelativeLayout soundLayout;
	private RelativeLayout vibrateLayout;
	private EmModel settingsModel;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_mine_setting);
	}

	@Override
	protected void onInit() {
		private_setting_btn = (RelativeLayout) findViewById(R.id.private_setting_btn);
		modify_password_btn = (RelativeLayout) findViewById(R.id.modify_password_btn);
		address_manage_btn = (RelativeLayout) findViewById(R.id.address_manage_btn);
		about_us_btn = (RelativeLayout) findViewById(R.id.about_us_btn);
		private_setting_btn.setOnClickListener(this);
		modify_password_btn.setOnClickListener(this);
		address_manage_btn.setOnClickListener(this);
		about_us_btn.setOnClickListener(this);
		cacheTv = (TextView)findViewById(R.id.cacheTv);
		
		notification = (SwitchButton) findViewById(R.id.switch_notification);
		notification.setOnChangedListener(this);
		sound = (SwitchButton) findViewById(R.id.switch_sound);
		sound.setOnChangedListener(this);
		vibrate = (SwitchButton) findViewById(R.id.switch_vibrate);
		vibrate.setOnChangedListener(this);
		soundLayout = (RelativeLayout)findViewById(R.id.sound_layout);
		vibrateLayout = (RelativeLayout)findViewById(R.id.vibrate_layout);
	}

	@Override
	protected void onData() {
		size = CacheClearUtil.getCachSize(this);
		cacheTv.setText("("+CacheClearUtil.formetFileSize(size)+")");
		
		settingsModel = EMHelper.getInstance().getModel();
		// 震动和声音总开关，来消息时，是否允许此开关打开
		if (settingsModel.getSettingMsgNotification()) {
			notification.setCheck(true);
		} else {
			notification.setCheck(false);
			soundLayout.setVisibility(View.GONE);
			vibrateLayout.setVisibility(View.GONE);
		}

		// 是否打开声音
		if (settingsModel.getSettingMsgSound()) {
			sound.setCheck(true);
		} else {
			sound.setCheck(false);
		}

		// 是否打开震动
		if (settingsModel.getSettingMsgVibrate()) {
			vibrate.setCheck(true);
		} else {
			vibrate.setCheck(false);
		}

		// 是否打开扬声器
//		if (settingsModel.getSettingMsgSpeaker()) {
//			chat.setCheck(true);
//		} else {
//			chat.setCheck(false);
//		}
	}
	
	public void clearCache(View v){
		new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
		.setTitleText("确认清除缓存:"+CacheClearUtil.formetFileSize(size))
		.setCancelText("取消")
		.setConfirmText("确定")
		.showCancelButton(true)
		.setCancelClickListener(new OnSweetClickListener() {
			@Override
			public void onClick(SweetAlertDialog mDialog) {
				mDialog.dismissWithAnimation();
			}
		})
		.setConfirmClickListener(new OnSweetClickListener() {
			@Override
			public void onClick(SweetAlertDialog mDialog) {
				CacheClearUtil.clearCach(SettingActivity.this);
				mDialog.dismissWithAnimation();
				cacheTv.setText("("+CacheClearUtil.formetFileSize(0)+")");
			}
		}).show();
	}
	
	public void onBack(View v){
		finish();
	}
	
	public void onLogout(View v){
		showProgressWithState("正在注销...");
		EMHelper.getInstance().logout(true, new EMCallBack() {
			
			@Override
			public void onSuccess() {
				mSpUtil.clearSharedPreferences();
				startActivity(LoginActivity.class);
				ActivitiesStack.getInstance().popAll(true);
			}
			
			@Override
			public void onProgress(int arg0, String arg1) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onError(int arg0, String arg1) {
				showErrorWithState();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.private_setting_btn:
			break;
		case R.id.modify_password_btn:
			startActivity(MyAmendPwdActivity.class);
			break;
		case R.id.address_manage_btn:
			startActivity(AddressManageActivity.class);
			break;
		case R.id.about_us_btn:
			startActivity(AboutActivity.class);
			break;
		default:
			break;
		}
	}
	
	public void switchNotification(View v) {
		if (notification.isChoose()) {
			notification.setCheck(false);
			settingsModel.setSettingMsgNotification(false);
			soundLayout.setVisibility(View.GONE);
			vibrateLayout.setVisibility(View.GONE);
		} else {
			notification.setCheck(true);
			settingsModel.setSettingMsgNotification(true);
			soundLayout.setVisibility(View.VISIBLE);
			vibrateLayout.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void OnChanged(View v, boolean checkState) {
		switch (v.getId()) {
		case R.id.switch_notification:
			if (!checkState) {
				notification.setCheck(false);
				settingsModel.setSettingMsgNotification(false);
				soundLayout.setVisibility(View.GONE);
				vibrateLayout.setVisibility(View.GONE);
			} else {
				notification.setCheck(true);
				settingsModel.setSettingMsgNotification(true);
				soundLayout.setVisibility(View.VISIBLE);
				vibrateLayout.setVisibility(View.VISIBLE);
			}
			break;
		case R.id.switch_sound:
			if (!checkState) {
				sound.setCheck(false);;
			    settingsModel.setSettingMsgSound(false);
			} else {
				sound.setCheck(true);;
			    settingsModel.setSettingMsgSound(true);
			}
			break;
		case R.id.switch_vibrate:
			if (!checkState) {
				vibrate.setCheck(false);
			    settingsModel.setSettingMsgVibrate(false);
			} else {
				vibrate.setCheck(true);
			    settingsModel.setSettingMsgVibrate(true);
			}
			break;
		default:
			break;
		}
	}
}
